<?php

namespace app\models;

use Yii;

class CooperateApp extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'cooperate_app';
    }
    public function dingdingToken(){

    }
	/**
	 * 获取app_id的应用配置
	 * @author LiShaoWei
	 * @access public
	 * @param mixed $appId 应用id
     * @param mixed $type 应用类型
	 * @since 1.0
	 * @return array
	 */
    public function findPerate ($appId, $type)
    {
        return CooperateApp::find()->where(array('app_id' => $appId, 'type' => $type))->one();
    }
    /**
     * 插入的应用配置
     * @author LiShaoWei
     * @access public
     * @param mixed $data 应用内容数组
     * @since 1.0
     * @return array
     */
    public function insertPerate($data=null)
    {
        return CooperateApp::find()->createCommand()->insert('cooperate_app',$data)->execute();
    }

    /**
     * 更新的应用配置
     * @author LiShaoWei
     * @access public
     * @param mixed $outPut 应用内容数组
     * @param mixed $cooperateAppId 更新判断
     * @param mixed $appId 应用ID
     * @since 1.0
     * @return array
     */
    public function updatePerate($outPut, $cooperateAppId, $appId)
    {
        return CooperateApp::find()->createCommand()->update('cooperate_app', array('output_token' => $outPut), array('app_id' => $appId, 'id' => $cooperateAppId))->execute();
    }

    /**
     * 微信授权配置验证
     * @author LiShaoWei
     * @access public
     * @param mixed $cooperateAppId 配置id
     * @since 1.0
     * @return array
     */
    public function cooperateConfig ($cooperateAppId, $appId)
    {
        //$CooperateAppModel = new CooperateApp();
        $cooperateApp = $this->findByCooperateId($appId, $cooperateAppId);
        if ($cooperateApp && $cooperateApp['type'] == 'WECHAT') {
            return json_decode($cooperateApp['output'], true);
        }
        return FALSE;
    }
    /**
     * 按appid和cooperateappId查找
     * @author 黄宇翔
     * @access public
     * @param mixed $cooperateAppId 配置id
     * @param mixed $appId 应用iD
     * @since 1.0
     * @return array
     */
    public function findByCooperateId($type){
        return CooperateApp::find()->where(array('type' => $type))->one();
    }

    public function findByCooperateIdAndType($appId, $type, $cooperateappId){
        return CooperateApp::find()->where(array('id' => $cooperateappId, 'type'=>$type, 'app_id' => $appId))->one();
    }
    /**
     * 创建分组
     * @author LiShaoWei
     * @access public
     * @param mixed $appId 应用iD
     * @param mixed $cooperateAppData 图片地址
     * @since 1.0
     * @return array
     */
    public function createCooperateApp ($cooperateAppData)
    {
        $uuidClass = new \uuid();
        $ruleId = $uuidClass->generateuuid();
        $data = array(
            'id' => $ruleId,
            'output_token' => $cooperateAppData['output_token'],
            'sign' => $cooperateAppData['sign'],
            'type'=>'DINGDING',
            'create_time' => time()
        );
        if($this->findByCooperateId('DINGDING')){
            return CooperateApp::find()->createCommand()->update('cooperate_app', array('output_token' => $cooperateAppData['output_token']), array('sign' => $cooperateAppData['sign']))->execute();
        }
        return CooperateApp::find()->createCommand()->insert('cooperate_app',$data)->execute();
    }
    /**
     *按照分页读取信息
     * @author LiShaoWei
     * @param mixed $appId 应用id
     * @param mixed $limit 分页数
     * @param mixed $pageno 分页数
     * @access public
     * @since 1.0
     * @return boolean
     */
    public function findByItems ( $appId, $limit, $pageno)
    {
        $storeCount = $this::find()->where(['app_id' => $appId])->count('id');
        $pageno = ($pageno - 1) * $limit;
        if ($data['items'] = $this::find()->where(['app_id' => $appId])->limit($limit)->offset($pageno)->orderBy('create_time')->all()) {
            $data['pageno'] = ceil((1 + $pageno) / $limit);
            $data['total'] = ceil($storeCount / $limit);
            return $data;
        }
        return FALSE;
    }
    public function delteteCooperateApp($cooperate_app_id,$appId){
        return $this::find()->createCommand()->delete('cooperate_app', ['id' => $cooperate_app_id,'app_id' => $appId])->execute();
    }

    public function findOutputId($appId, $outputId)
    {
        return CooperateApp::find()->where(array('app_id' => $appId, 'output_id' => $outputId))->one();
    }
}
